/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.common.json;

import com.entities.EventLibrary;
import com.entities.EventRegistration;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

/**
 *
 * @author Tuan Long
 */
public class JsonModel {
    
    /**
     *@JsonModel.convertDatetimeToString: 
     * output: string date with format: MM/dd/yyyy HH:mm:ss
     * @return  String
     * @param Date
     */
    private static String convertDatetimeToString(Date time){
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String result = df.format(time);
        return result;
    }
    /**
     *@JsonModel.convertStringToDatetime:
     * format: yyyy/MM/dd HH:mm:ss
     * @return  String
     * @param Date 
     */
    private static Date convertStringToDatetime(String text){        
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            Date date = df.parse(text);
            return date;
        } catch (ParseException ex) {
            Logger.getLogger(JsonModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    /**
     * @JsonModel.eventMessageSender : input is EventLibrary object, output is JsonObject.
     * @param obj
     * @return JSONObject
     */  
    
    public static JSONObject eventMessageSender(EventLibrary obj){        
        String start = convertDatetimeToString(obj.getTimeStart());
        String end = convertDatetimeToString(obj.getTimeEnd());
        JSONObject message = new JSONObject();
        message.put("eventId", obj.getEventId());
        message.put("name", obj.getName());
        message.put("description", obj.getDescription());
        message.put("eventType", obj.getEventType());
        message.put("start", start);
        message.put("end", end);
        return message;   
    }  
    /**
     * @JsonModel.eventsDataTableMessageSender : input is EventLibrary object and number employees were registered
     * , output is JsonObject. (for method: EventLibraryFacade.fillEventsDataTable())
     * @param obj
     * @param registered
     * @return JSONObject
     */  
    
    public static JSONObject eventsDataTableMessageSender(EventLibrary obj, int registered){        
        String start = convertDatetimeToString(obj.getTimeStart());
        String end = convertDatetimeToString(obj.getTimeEnd());
        JSONObject message = new JSONObject();
        message.put("eventId", obj.getEventId());
        message.put("name", obj.getName());
        message.put("description", obj.getDescription());
        message.put("eventType", obj.getEventType());
        message.put("start", start);
        message.put("end", end); 
        message.put("registered", registered);
        return message;          
    }  
    /**
     * @JsonModel.eventMessageReader : input is JsonObject, output is EventLibrary object.
     * @param json
     * @return EventLibrary
     */
    public static EventLibrary eventMessageReader(JSONObject json){        
        String idstr = json.get("eventId").toString();
        int id = Integer.parseInt(idstr);
        String name = json.get("name").toString();
        String des = json.get("description").toString();
        String type = json.get("eventType").toString();
        String start = json.get("start").toString();
        String end = json.get("end").toString();
        Date timeS = convertStringToDatetime(start);
        Date timeE = convertStringToDatetime(end);
        EventLibrary obj = new EventLibrary(id, name, des, type, timeS, timeE);        
        return obj;            
    }
    /**
     * @JsonModel.JsonDecoder : input is String values send by client
     * @param  jsonstr
     * @return JSONObject
     */
    public static JSONObject JsonDecoder(String jsonstr){
        try {
            JSONParser  parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonstr);
            return json;
        } catch (org.json.simple.parser.ParseException ex) {
            Logger.getLogger(JsonModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * @JsonModel.eventRegistrationSender : 
     *  convert The data of select by stored procedure: sp_fillRegisteredTable To JSONObject
     * @param er
     * @param  eventname
     * @return JSONObject
     */
    public static JSONObject eventRegistrationSender(EventRegistration er,String eventname) {
        JSONObject message = new JSONObject();
        String registered = convertDatetimeToString(er.getTimeRegisted());
        String username = er.getUsername().getUsername();
        message.put("eventname", eventname);
        message.put("username", username);
        message.put("registered", registered);
        return message;
    }
    
    /**
     * @param  username
     * @param  total
     * @return JSONObject
     */
    public static JSONObject eventTotalRegistrationSender(String username, String total){
        JSONObject message= new JSONObject();
        message.put("username", username);
        message.put("total", total);
        return message;
    }
}
